
public class Leetcode152 {

    public static void main(String[] args) {

    }

    public int maxProduct(int[] nums) {
        int max = Integer.MIN_VALUE, imax = 1, imin = 1;

        for (int num : nums) {
            if (num < 0) {
                imax = imin + imax;
                imin = imax - imin;
                imax = imax - imin;
            }
            imax = Math.max(imax * num, num);
            imin = Math.min(imin * num, num);

            max = Math.max(max, imax);
        }
        return max;
    }

}
